package io.renren.modules.jxgkphone.service.Impl;


import io.renren.modules.jxgkphone.dao.StuInfoMapper;
import io.renren.modules.jxgkphone.entity.StudentInfo;
import io.renren.modules.jxgkphone.entity.TeacherUser;
import io.renren.modules.jxgkphone.service.StuInfoService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StuInfoServiceImpl implements StuInfoService {
    @Autowired
    StuInfoMapper stuInfoMapper;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public StudentInfo GetStuInfo(String student_school_id){

        StudentInfo studentInfo=stuInfoMapper.GetStuInfo(student_school_id);
                 return studentInfo;
    }
    public void AddStudentLeader(String username,
                          String password,
                          String salt,
                          int status,
                          int create_user_id,
                          String full_name){
        stuInfoMapper.AddStudentLeader(username,password,salt,status,create_user_id,full_name);
    }
    public void UpdatePassword(String salt, String password){
        stuInfoMapper.UpdatePassword(salt,password);
    }
    public String findUserIdByUserName(String username){
        return  stuInfoMapper.findUserIdByUserName(username);
    }
    public void InsertUserRole(String user_id,String role_id){
        stuInfoMapper.InsertUserRole(user_id,role_id);
    }
    public List<TeacherUser> findTeacherInfo(){
        return stuInfoMapper.findTeacherInfo();
    }
    public String findRoleByRoleIdAndUserId(String userId,String roleId){
        return stuInfoMapper.findRoleByRoleIdAndUserId(userId,roleId);
    }

    @Override
    public void insertStudent() {
        List<StudentInfo> studentInfos=stuInfoMapper.getAllStudent();
        List<StudentInfo> studentInfoList=new ArrayList<>();
        int i=0;
        for(StudentInfo studentInfo:studentInfos)
        {
            try {
                SysUserEntity user = sysUserService.queryByUserName(studentInfo.getStudentSchoolId());
                if (user == null) {
                    user = new SysUserEntity();
                    user.setUsername(studentInfo.getStudentSchoolId());
                    user.setStatus(0);
                    user.setPassword("c44130ed4ee0e3e0e071b56dcecfb9b97d2362c32d0283bf35f8392e6706e10f");
                    user.setSalt("enIKZm6Zqc9Xod5JpqAs");
                    user.setFullName(studentInfo.getStudentName());
                    sysUserDao.save(user);
                    studentInfoList.add(studentInfo);
                    i++;
                    System.out.println("已经插入了"+i+"条");
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        System.out.println(studentInfoList);
    }
    public void DeleteUserByUserName(String username){
        stuInfoMapper.DeleteUserByUserName(username);
    }
    public void DeleteUserRoleByUserId(String userId){
        stuInfoMapper.DeleteUserRoleByUserId(userId);
    }

}
